<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :tree-node="company" :is-root="true" @add-depts="handleAddDepts" />

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <adddept
      ref="addDeptDialog"
      :show-dialog.sync="showDialog"
      :node-data="nodeData"
      :depts-list="deptsList"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index'
import treeTools from '@/views/departments/tree-tools.vue'
import { getDepartmentsApi } from '@/api/departments'
import adddept from '@/views/departments/add-dept.vue'

export default {
  name: 'Departments',
  components: {
    treeTools,
    adddept
  },
  data() {
    return {
      deptsList: [],
      nodeData: {},
      showDialog: false,
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$refs.addDeptDialog.getEmployeeSimple()
      this.$refs.addDeptDialog.getDepartDetail(nodeData.id)
    },
    handleAddDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
    },
    async getDepartments() {
      const { data } = await getDepartmentsApi()
      this.company = {
        name: data.companyName,
        manager: '负责人',
        id: ''
      }
      this.departs = tranListToTreeData(data.depts, '') // 需要将其转换成树形结构
      this.deptsList = data.depts // 列表式数据
      // console.log(data)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
